package com.kitty.game.equip.handler;

import com.kitty.game.FieldValuePosConst;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.common.model.Reason;
import org.springframework.stereotype.Component;

/**
 * 魂器 技能升级
 */
@Component("EquipHunQiSkillUpgradeHandler")
public class EquipHunQiSkillUpgradeHandler extends UpgradeHandler {
    private final static String ERROR_ITEM = "放入的物品不是魂器，请重新选择。";
    private final static String NOT_MATCH = "请放入同种类魂器。";
    private final static String GOLD_NOT_ENOUGH = "你的元宝不足。";
    private final static int NEED_GOLD = 466;
    private final static String SUCCESS = "魂器技能升级成功。";

    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip mainHunQi = (RoleEquip) params[0];
        RoleEquip firstEquip = (RoleEquip) params[1];
        RoleEquip secondEquip = (RoleEquip) params[2];
        /**删除魂器*/
        equipService.delRoleEquip(firstEquip,role);
        equipService.delRoleEquip(secondEquip,role);
        /**扣除元宝 todo 好像是固定扣除466元宝*/
        roleService.subtractGold(role,NEED_GOLD, Reason.REFINE_HUNQI);
        int specialSkillLevel = mainHunQi.getEquipValue(FieldValuePosConst.HUNQI_SKILL_LEVEL);
        mainHunQi.setEquipValue(FieldValuePosConst.HUNQI_SKILL_LEVEL,specialSkillLevel+1);
        refreshRoleEquip(role, mainHunQi);
        pushOperateResult(role,UPGRADE_SUCCESS);
        return SUCCESS;
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String[] params = param.split("\\|");
        RoleEquip mainHunQi = equipService.getRoleEquipByPos(role, upgradePos);
        RoleEquip firstEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[0]));
        RoleEquip secondEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[1]));

        /**判断是不是法宝*/
        if (!isHunQi(mainHunQi, firstEquip, secondEquip)) {
            return ERROR_ITEM;
        }
        /**判断元宝是否足够*/
        if(role.getGold()<NEED_GOLD){
            return GOLD_NOT_ENOUGH;
        }
        return doUpgrade(role,mainHunQi,firstEquip,secondEquip);
    }

    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    @Override
    public String getUpdateTypeStr() {
        return "hun_qi_";
    }

    /**
     * 判断传入的是不是魂器
     */
    private boolean isHunQi(RoleEquip... roleEquips) {
        for (RoleEquip roleEquip : roleEquips) {
            if (roleEquip == null || !roleEquip.isHunQi()) {
                return false;
            }
        }
        return true;
    }
}
